package com.ruike.alisurface.utils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.widget.ImageView;

import com.blankj.utilcode.util.FileUtils;
import com.ruike.alisurface.Constant;
import com.voodoo.lib_logcat.L;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class BitmipUtil {
    static    String TAG="BitmipUtil";
    /**
     * 获取正确缩放裁剪适应IamgeView的Bitmap
     * @param imageView
     * @param bitmap
     * @return
     */
    public static Bitmap createFitBitmap(ImageView imageView, Bitmap bitmap) {
        Log.i(TAG, "createFitBitmap<---------------------");
        int widthTarget = imageView.getWidth();
        int heightTarget = imageView.getHeight();
        int widthBitmap = bitmap.getWidth();
        int heightBitmap = bitmap.getHeight();
        Log.i(TAG, "widthTarget = " + widthTarget );
        Log.i(TAG, "heightTarget = " + heightTarget );
        Log.i(TAG, "widthBitmap = " + widthBitmap );
        Log.i(TAG, "heightBitmap = " + heightBitmap );
        Bitmap result = null;
        if( widthBitmap >= widthTarget && heightBitmap >= heightTarget ){
            result = createLargeToSmallBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
        }
        else if( widthBitmap >= widthTarget && heightBitmap < heightTarget ){
            Bitmap temp = createLargeWidthToEqualHeightBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
        }
        else if( widthBitmap < widthTarget && heightBitmap >= heightTarget ){
            Bitmap temp = createLargeHeightToEqualWidthBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createLargeToSmallBitmap(temp.getWidth(), temp.getHeight(), widthTarget, heightTarget, temp);
        }
        else{
            Bitmap temp = createSmallToEqualBitmap(widthBitmap, heightBitmap, widthTarget, heightTarget, bitmap);
            result = createFitBitmap(imageView, temp);
        }
        Log.i(TAG, "createFitBitmap--------------------->");
        return result;
    }



    public static Bitmap createLargeToSmallBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){
        Log.i(TAG, "createLargeToSmallBitmap<---------------------");
        Log.i(TAG, "widthTarget = " + widthTarget );
        Log.i(TAG, "heightTarget = " + heightTarget );
        Log.i(TAG, "widthBitmap = " + widthBitmap );
        Log.i(TAG, "heightBitmap = " + heightBitmap );
        int x = (widthBitmap - widthTarget) / 2;
        int y = (heightBitmap - heightTarget) / 2;
        Log.i(TAG, "createLargeToSmallBitmap--------------------->");
        return Bitmap.createBitmap(bitmap, x, y, widthTarget, heightTarget);
    }

    private static Bitmap createLargeWidthToEqualHeightBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        Log.i(TAG, "createLargeWidthToEqualHeightBitmap<---------------------");
        Log.i(TAG, "widthTarget = " + widthTarget );
        Log.i(TAG, "heightTarget = " + heightTarget );
        Log.i(TAG, "widthBitmap = " + widthBitmap );
        Log.i(TAG, "heightBitmap = " + heightBitmap );
        double scale = ( heightTarget * 1.0 ) / heightBitmap;
        Log.i(TAG, "createLargeWidthToEqualHeightBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale) , heightTarget, false);
    }

    private static Bitmap createLargeHeightToEqualWidthBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        Log.i(TAG, "createLargeHeightToEqualWidthBitmap<---------------------");
        Log.i(TAG, "widthTarget = " + widthTarget );
        Log.i(TAG, "heightTarget = " + heightTarget );
        Log.i(TAG, "widthBitmap = " + widthBitmap );
        Log.i(TAG, "heightBitmap = " + heightBitmap );
        double scale = ( widthTarget * 1.0 ) / widthBitmap;
        Log.i(TAG, "createLargeHeightToEqualWidthBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, widthTarget , (int)(heightTarget * scale), false);
    }

    private static Bitmap createSmallToEqualBitmap(int widthBitmap, int heightBitmap, int widthTarget, int heightTarget, Bitmap bitmap){

        Log.i(TAG, "createSmallToEqualBitmap<---------------------");
        Log.i(TAG, "widthTarget = " + widthTarget );
        Log.i(TAG, "heightTarget = " + heightTarget );
        Log.i(TAG, "widthBitmap = " + widthBitmap );
        Log.i(TAG, "heightBitmap = " + heightBitmap );
        double scaleWidth = ( widthTarget * 1.0 ) / widthBitmap;
        double scaleHeight = ( heightTarget * 1.0 ) / heightBitmap;
        double scale = Math.min(scaleWidth, scaleHeight);
        Log.i(TAG, "createSmallToEqualBitmap--------------------->");
        return Bitmap.createScaledBitmap(bitmap, (int)(widthBitmap * scale), (int)(heightBitmap * scale), false);
    }
    /**
     * 裁剪
     *
     * @param path 原图 路径
     * @return 裁剪后的图像
     */
    public static String cropBitmap(String path) {
        Bitmap bitmap = BitmapFactory.decodeFile(path);
        int w = bitmap.getWidth(); // 得到图片的宽，高
        int h = bitmap.getHeight();
        int heightTarget = 712;
        int widthTarget =950 ;
        Bitmap cropmip = BitmipUtil.createLargeToSmallBitmap(w, h, widthTarget, heightTarget, bitmap);
        return saveFile(cropmip);
    }


    /**
     * 保存图片
     *
     * @param bm
     */
    public static  String saveFile(Bitmap bm) {
        long startTime = System.currentTimeMillis();
        Date date = new Date(startTime);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd-HHmmss");
        String title = format.format(date);
        String fileName = "mIDcrop_"+title + ".jpg";

        String subForder = Constant.FilePath.IDCARD_PATH;
        File foder = new File(subForder);
        if (!foder.exists()) {
            foder.mkdirs();
        }else{
            FileUtils.deleteAllInDir(foder.getAbsolutePath());
        }
        try {
            File myCaptureFile = new File(subForder, fileName);
            if (!myCaptureFile.exists()) {
                myCaptureFile.createNewFile();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            if (bm != null && !bm.isRecycled()) {
                bm.recycle();
                bm = null;
            }
            bos.flush();
            bos.close();
            return myCaptureFile.getAbsolutePath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
